package com.lemon.api.auto.ApiExecutor;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.testng.annotations.Test;

import java.io.IOException;
import java.util.*;
/*
json提交器
 */
@Test(enabled = false)
public class JsonSubmitApiExecutor {
    public static void doGet(String url,String param,String charset){
        Map<String,String> map = (Map<String, String>) JSONObject.parse(param);
        Set<String> paramNames = map.keySet();
        int flag=0;
        for (String paramName : paramNames) {
            String paramValue = map.get(paramName);
            if (flag == 0) {
                url +=("?"+paramName+"="+paramValue);
            }else {
                url+=("&"+paramName+"="+paramValue);
            }
            flag++;
        }

        //接口url地址
/*        String url="http://test.lemonban.com/futureloan/mvc/api/member/login";
        String mobilephone="13517315120";
        String pwd="123456";
        url+=("?mobilephone="+mobilephone+"+&pwd="+pwd);*/
        //创建请求对象
        HttpGet httpGet = new HttpGet(url);
        //创建http客户端对象
        HttpClient httpClient = HttpClients.createDefault();
        //添加请求头指定数据格式
        httpGet.addHeader("Content-Type","application/json;charest=UTF-8");
        //发包
        try {
            //调用完接口获取到服务器的响应
            HttpResponse httpResponse = httpClient.execute(httpGet);
            //从响应对象中取出状态码
            int code = httpResponse.getStatusLine().getStatusCode();
            //从响应对象中取出响应报文
            String result = EntityUtils.toString(httpResponse.getEntity());
            //从响应对象中取出响应头
            String responseHeadersString = Arrays.toString(httpResponse.getAllHeaders());
            System.out.println("状态码:["+code+"]");
            System.out.println("响应报文:["+result+"]");
            System.out.println("响应头:["+responseHeadersString+"]");

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static void doPost(String url,String param,String charset){
        //创建请求对象
        HttpPost httpPost = new HttpPost(url);
        //创建http客户端对象
        HttpClient httpClient = HttpClients.createDefault();
        //发包
        try {
//            List<BasicNameValuePair> paramters = new ArrayList<BasicNameValuePair>();
            //设置请求头
            httpPost.addHeader(new BasicHeader("Content-Type","application/json;charest=UTF-8"));
            //准备参数，将参数设置到请求体中
            //UrlEncodedFormEntity:以表单的方式传递数据（mobilephone=13517315120&pwd=123456）
            httpPost.setEntity(new StringEntity(param,charset));
            //调用完接口获取到服务器的响应
            HttpResponse httpResponse = httpClient.execute(httpPost);
            //从响应对象中取出状态码
            int code = httpResponse.getStatusLine().getStatusCode();
            //从响应对象中取出响应报文
            String result = EntityUtils.toString(httpResponse.getEntity());
            //从响应对象中取出响应头
            String responseHeadersString = Arrays.toString(httpResponse.getAllHeaders());
            System.out.println("状态码:["+code+"]");
            System.out.println("响应报文:["+result+"]");
            System.out.println("响应头:["+responseHeadersString+"]");

        } catch (IOException e) {
            e.printStackTrace();
        }

    }
@Test
    public void test() {
        String url="http://test.lemonban.com/futureloan/mvc/api/member/register\n";
        String param="{\"mobilephone\":\"19000000000\",\"pwd\":\"\"}\n";
        List<NameValuePair> paramters=new ArrayList<NameValuePair>();
        paramters.add(new BasicNameValuePair("username","system"));
        paramters.add(new BasicNameValuePair("password","103"));
        paramters.add(new BasicNameValuePair("benobody","True"));
        String charset ="UTF-8";
//        doPost(url,paramters,charset);
        JsonSubmitApiExecutor.doPost(url,param,charset);


    }

    public static void main(String[] args) {
        String url="http://test.lemonban.com/futureloan/mvc/api/member/register";
        String param="{\"mobilephone\":\"19000000000\",\"pwd\":\"123\"}\n";
        String charset ="UTF-8";
        Map<String,String> map = (Map<String, String>) JSONObject.parse(param);
        Set<String> paramNames = map.keySet();
        int flag=0;
        for (String paramName : paramNames) {
            String paramValue = map.get(paramName);
            if (flag == 0) {
                url +=("?"+paramName+"="+paramValue);
            }else {
                url+=("&"+paramName+"="+paramValue);
            }
            flag++;
        }
        System.out.println(url);
    }

}
